/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.dao.cd.impl;

import java.io.File;

import org.jdom.Element;

import who.tmap.bo.BizObject;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.impl.CartographicDisplayFactory;
import who.tmap.common.TMapException;
import who.tmap.dao.TMapDAOException;
import who.tmap.dao.cd.CartographicDisplayDAO;
import who.tmap.dao.impl.BaseDAOImpl;
import who.tmap.util.MiscHelper;

/**
 * This class is not abstract because of getCDType method
 * @author stl
 *
 */
public abstract class BaseCartographicDisplayDAOImpl extends BaseDAOImpl 
											implements CartographicDisplayDAO {
	
	protected static final String VERSION ="0.7";
	
	protected static final CartographicDisplayFactory cdf =CartographicDisplayFactory.getInstance();
	
	protected MiscHelper miscHelper =new MiscHelper();
	
	BaseCartographicDisplayDAOImpl() {
		super();
	}
		
	/**
	 * Load CD file
	 */
	public BizObject load(String fileName) throws TMapDAOException {
		CartographicDisplay cd =null;
        
        try {
	        Element rootElt =getRootElement( fileName );
	        
	        cd =(CartographicDisplay)loadXml( rootElt );
	        cd.setFile( new File( fileName ) );
	        	        
        } catch( Exception e ) {
	        throw new TMapDAOException( e );
        }
        
        return cd;
	}	
	
	protected CartographicDisplay getCD( Element elt ) throws TMapException {
		CartographicDisplay cd =null;
		
		if ( elt.getChild( "ColorPattern" ) != null ) {
			cd =cdf.get( CDTypes.COLOR_PATTERN );
		} else if ( elt.getChild( "ProportionalSymbols" ) != null ) {
			cd =cdf.get( CDTypes.PROPORTIONAL_SYMBOLS );
		} else if ( elt.getChild( "RandomDotDensity" ) != null ) {
			cd =cdf.get( CDTypes.DOT_DENSITY );
		} else if ( elt.getChild( "PointBasic" ) != null ) {
			cd =cdf.get( CDTypes.POINT_BASIC );
		} else if ( elt.getChild( "PointDataDrivenSize" ) != null ) {
			cd =cdf.get( CDTypes.POINT_DATA_DRIVEN_SIZE );
		} else if ( elt.getChild( "PointDataDrivenSymbol" ) != null ) {
			cd =cdf.get( CDTypes.POINT_DATA_DRIVEN_SYMBOL );
		} else if ( elt.getChild( "PointDataDrivenSymbolAndSize" ) != null ) {
			cd =cdf.get( CDTypes.POINT_DATA_DRIVEN_SYMBOL_AND_SIZE );
		}
		
		return cd;
	}
	
	private Element getRootElement() {
		return new Element( ROOT_ELT );
	}
	
	public Element saveXml(BizObject object) throws TMapDAOException {
		object.setDirty( false );
		
		return getRootElement();
	}
		
	protected String getNonNullString( String str ) {
		if ( str != null ) {
			return str;
		} else {
			return "";
		}
	}
	
	protected String getNullStringIfEmpty( String str ) {
		if ( "".equals( str ) ) {
			return null;
		} else {
			return str;
		}
	}

}
